package com.csqf.booksys.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.csqf.booksys.entity.Book;
import com.csqf.booksys.entity.PageBean;
import com.csqf.booksys.service.IBookService;
import com.csqf.booksys.service.impl.BookServiceImpl;
import com.csqf.booksys.utils.DateUtils;

/**
 *图书操作的Servlet  控制层
 */
@WebServlet("/book")
public class BookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	IBookService bookServiceImpl = new BookServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    //1、统一编码设置
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		//2、获取请求的动作
		String action = request.getParameter("action");
		if("add".equals(action)){
			addBook(request,response);
		}if("upd".equals(action)){
			updBook(request,response);
		}if("all".equals(action)){
			allBook(request,response);
		}if("update".equals(action)){
			updateBook(request,response);
		}if("del".equals(action)){
			delBook(request,response);
		}
		
	}
	
	protected void addBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1、获取浏览器提交的数据
		String name = request.getParameter("name");
		String author = request.getParameter("author");
		String publish = request.getParameter("publish");
		String price = request.getParameter("price");
		String crtTime = request.getParameter("crtTime");
		System.out.println(crtTime);
		Book book = new Book(null,name,author,publish,Double.parseDouble(price),DateUtils.strToUtilDate(crtTime));
		//调用业务层
		int flag = bookServiceImpl.addBook(book);
		if(flag > 0){
			//成功则获取所有数据 并显示  重定向
			System.out.println("添加成功！");
			response.sendRedirect(request.getContextPath()+"/book?action=all");
		}else{
			System.out.println("添加失败！");
		}
	}
	
	protected void updateBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1、获取浏览器提交的数据
		String bid = request.getParameter("bid");
		String name = request.getParameter("name");
		String author = request.getParameter("author");
		String publish = request.getParameter("publish");
		String price = request.getParameter("price");
		String crtTime = request.getParameter("crtTime");
		System.out.println(crtTime);
		Book book = new Book(Integer.parseInt(bid),name,author,publish,Double.parseDouble(price),DateUtils.strToUtilDate(crtTime));
		//调用业务层
		int flag = bookServiceImpl.updBook(book);
		if(flag > 0){
			//成功则获取所有数据 并显示  重定向
			System.out.println("修改成功！");
			response.sendRedirect(request.getContextPath()+"/book?action=all");
		}else{
			System.out.println("修改失败！");
		}
	}
	protected void delBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1、获取浏览器提交的数据
		String bid = request.getParameter("bid");
		//调用业务层
		int flag = bookServiceImpl.delBook(Integer.parseInt(bid));
		if(flag > 0){
			//成功则获取所有数据 并显示  重定向
			System.out.println("删除成功！");
			response.sendRedirect(request.getContextPath()+"/book?action=all");
		}else{
			System.out.println("删除失败！");
		}
	}
	/*
	 * 分页查询
	 */
	protected void allBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//0、获取当前页数
		String currentPage = request.getParameter("currentPage");
		if(currentPage == null){
			currentPage = "1";
		}
		//1、创建分页PageBean
		PageBean<Book> pb = new PageBean<Book>();
		pb.setCurrentPage(Integer.parseInt(currentPage));
		//2、查询
		bookServiceImpl.findAll(pb);
		//3、处理
		request.setAttribute("pageBean", pb);
		//4、转发到目标页面
		request.getRequestDispatcher("/pages/all.jsp").forward(request, response);
	}
	
	/*protected void allBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//调用业务层
		List<Book> list = bookServiceImpl.findAll();
		if(list != null){
			request.setAttribute("list", list);
			request.getRequestDispatcher("/pages/all.jsp").forward(request, response);
		}else{
			System.out.println("添加失败！");
			//request.setAttribute("msg", "当前没有任何数据！");
			response.sendRedirect(request.getContextPath()+"/pages/addBook.jsp");
		}
	}*/
	
	protected void updBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String bid = request.getParameter("bid");
		//根据bid查询数据
		Book book = bookServiceImpl.findById(Integer.parseInt(bid));
		request.setAttribute("book",book);
		//转发
		request.getRequestDispatcher("/pages/upd.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
